package com.yuanfuxing.boxuegu.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.yuanfuxing.boxuegu.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class RegisterActivity extends AppCompatActivity {
         EditText username;
         EditText password,mComfrimPassqord;

    public void back(View view){
        finish();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        username = (EditText) findViewById(R.id.et_user_name);
        password = (EditText) findViewById(R.id.et_psw);
        mComfrimPassqord = (EditText) findViewById(R.id.et_psw_again);
    }

    public void register(View view){

        if(TextUtils.isEmpty(username.getText().toString())){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(password.getText().toString())){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        } if(!password.getText().toString().equals(mComfrimPassqord.getText().toString())){
            Toast.makeText(this,"两次密码必须相同",Toast.LENGTH_SHORT).show();
            return;
        }


        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Register");
        params.addQueryStringParameter("username", username.getText().toString());
        params.addQueryStringParameter("password", password.getText().toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Loginmodel loginmodel = gson.fromJson(result,Loginmodel.class);
                if(loginmodel.code==200){
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(RegisterActivity.this,"注册失败"+loginmodel.msg,Toast.LENGTH_SHORT).show();
                }
                Log.i("LoginActivity","onSuccess result"+loginmodel.msg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("LoginActivity","onError isOnCallback"+ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
